package com.woniu.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.entity.TbJpPermission;
import com.woniu.resultSet.Result;
import com.woniu.web.service.TbJpPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sxs
 * info:权限表的增删改查,
 *
 */
@RestController
@RequestMapping("/permission")
@Api(value = "权限相关")
public class TbJpPermissionController {
    @Resource
    TbJpPermissionService permissionService;
    @GetMapping("/")
    @ApiOperation(value = "查看全部权限", notes = "This method for 查找所有权限")
    public Result findAllRoles(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="5") Integer pageSize,String title){
        Result result=new Result();

        try {
            Map map =new HashMap();
            PageHelper.startPage(pageNum,pageSize);
            List<TbJpPermission> allPermission = permissionService.findAllPermission(title);
            PageInfo<TbJpPermission> pageInfo=new PageInfo<>(allPermission);

            result.setObj(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/")
    @ApiOperation(value = "增加权限", notes = "This method for 增加权限")
    public Result addPermission(@RequestBody TbJpPermission tbJpPermission){
        Result result=new Result();
        try {

            permissionService.addPermission(tbJpPermission);
             result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @DeleteMapping("/{permissionId}")
    @ApiOperation(value = "删除权限", notes = "This method for 查找所有权限,删除权限")
    public Result delPermission(@PathVariable Integer permissionId){
        Result result=new Result();
        try {
            permissionService.delPermission(permissionId);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/delMore")
    @ApiOperation(value = "批量删除权限", notes = "This method for 查找所有权限,批量删除权限")
    public Result delRoleMore(@RequestBody Integer[] permissionIds){
        Result result=new Result();
        try {
            permissionService.delPermissionMore(permissionIds);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PutMapping("/")
    @ApiOperation(value = "修改权限", notes = "This method for 查找所有权限,修改权限")
    public Result updateRole(@RequestBody TbJpPermission tbJpPermission){
        Result result=new Result();
        try {
            permissionService.updatePermission(tbJpPermission);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
